var color = "red";
var alertBorderWidth = 10;

chrome.extension.onMessage.addListener(
  function(request, sender, sendResponse) {	
	if (request.isAlertDomain != null) {
		if (request.color != null) {
			color = request.color;
			localStorage.setItem("alertDomainColor", color);
		}
		
		if (request.alertBorderWidth != null) {
			alertBorderWidth = request.alertBorderWidth;
			localStorage.setItem("alertBorderWidth", alertBorderWidth);
		}
		
		if (request.alertPrimaryColor != null) {
			primaryColor = request.alertPrimaryColor;
			localStorage.setItem("alertPrimaryColor", primaryColor);
		}
		
		if (request.alertPy != null) {
			alertPy = request.alertPy;
			localStorage.setItem("alertPy", alertPy);
		}
		
		if (request.alertPx != null) {
			alertPx = request.alertPx;
			localStorage.setItem("alertPx", alertPx);
		}
		
		localStorage.setItem("isAlertDomain", request.isAlertDomain);
		window.location = request.requestingUrl;
	}
	
	if (request.requestType == "domain") {
		var hostName = location.host;
		sendResponse({"domainName": hostName});
	}
	
	if (request.requestType == "isSaved") {
		if (localStorage.getItem("isAlertDomain") != null && localStorage.getItem("isAlertDomain") == "true") {
			var savedColor = localStorage.getItem("alertDomainColor");
			var savedBorderWidth = localStorage.getItem("alertBorderWidth");
			var savedPrimaryColor = localStorage.getItem("alertPrimaryColor");
			var alertPx = localStorage.getItem("alertPx");
			var alertPy = localStorage.getItem("alertPy");
			sendResponse({"isSaved": "true", "savedColor": savedColor, "savedBorderWidth": savedBorderWidth, "alertPrimaryColor": savedPrimaryColor, "alertPx": alertPx, "alertPy": alertPy});
		} else {
			sendResponse({"isSaved": "false", "savedColor": null, "savedBorderWidth": null});
		}
	}
	
	if (request.requestType == "savedColor") {
		sendResponse({"savedColor": localStorage.getItem("alertDomainColor")});
	}
	
  });
  
if (localStorage.getItem("isAlertDomain") != null && localStorage.getItem("isAlertDomain") == "true") {
	if (localStorage.getItem("alertDomainColor") != null) {
		color = localStorage.getItem("alertDomainColor");
	}
	
	if (localStorage.getItem("alertBorderWidth") != null) {
		alertBorderWidth = localStorage.getItem("alertBorderWidth");
	}
	
	createAlertFrame(alertBorderWidth + "px", color);
}   
  
function createAlertFrame(alertFrameWidth, alertFrameColor) {

	var alertFrameZIndex = '999999999';

	var alertDivTop = document.createElement('div');
	alertDivTop.setAttribute('id', 'extensionAlertDivTop');
	alertDivTop.style.backgroundColor = alertFrameColor;
	alertDivTop.style.color = 'white';
	alertDivTop.style.fontWeight = 'bold';
	alertDivTop.style.fontSize = alertFrameWidth;
	alertDivTop.style.textAlign = 'center';
	alertDivTop.style.width = '100%';
	alertDivTop.style.height = alertFrameWidth;
	alertDivTop.style.position = 'fixed';
	alertDivTop.style.left = '0px';
	alertDivTop.style.right = '0px';
	alertDivTop.style.top = '0px';
	alertDivTop.style.zIndex = alertFrameZIndex;	
	
	var alertDivBottom = document.createElement('div');
	alertDivBottom.setAttribute('id', 'extensionAlertDivBottom');
	alertDivBottom.style.backgroundColor = alertFrameColor;
	alertDivBottom.style.width = '100%';
	alertDivBottom.style.height = alertFrameWidth;
	alertDivBottom.style.position = 'fixed';
	alertDivBottom.style.left = '0px';
	alertDivBottom.style.right = '0px';
	alertDivBottom.style.bottom = '0px';
	alertDivBottom.style.zIndex = alertFrameZIndex;

	var alertDivLeft = document.createElement('div');
	alertDivLeft.setAttribute('id', 'extensionAlertDivLeft');
	alertDivLeft.style.backgroundColor = alertFrameColor;
	alertDivLeft.style.width = alertFrameWidth;
	alertDivLeft.style.height = '100%';
	alertDivLeft.style.position = 'fixed';
	alertDivLeft.style.left = '0px';
	alertDivLeft.style.zIndex = alertFrameZIndex;
	
	var alertDivRight = document.createElement('div');
	alertDivRight.setAttribute('id', 'extensionAlertDivRight');
	alertDivRight.style.backgroundColor = alertFrameColor;
	alertDivRight.style.width = alertFrameWidth;
	alertDivRight.style.height = '100%';
	alertDivRight.style.position = 'fixed';
	alertDivRight.style.right = '0px';
	alertDivRight.style.zIndex = alertFrameZIndex;	
	
	document.body.insertBefore(alertDivTop, document.body.firstChild);
	document.body.insertBefore(alertDivBottom, document.body.firstChild);
	document.body.insertBefore(alertDivLeft, document.body.firstChild);
	document.body.insertBefore(alertDivRight, document.body.firstChild);
}